package top.lyc829.zzl_android.ui.index.chooseLocation;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;

import com.amap.api.location.AMapLocation;
import com.amap.api.location.AMapLocationClient;
import com.amap.api.location.AMapLocationClientOption;
import com.amap.api.location.AMapLocationListener;
import com.hjq.permissions.OnPermissionCallback;
import com.hjq.permissions.Permission;
import com.hjq.permissions.XXPermissions;
import com.jeremyliao.liveeventbus.LiveEventBus;
import com.xuexiang.citypicker.CityPicker;
import com.xuexiang.citypicker.adapter.OnLocationListener;
import com.xuexiang.citypicker.adapter.OnPickListener;
import com.xuexiang.citypicker.model.City;
import com.xuexiang.citypicker.model.HotCity;
import com.xuexiang.citypicker.model.LocateState;
import com.xuexiang.citypicker.model.LocatedCity;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import top.lyc829.zzl_android.R;
import top.lyc829.zzl_android.logic.bean.LiveEventMsg;
import top.lyc829.zzl_android.logic.util.Utils;

public class ChooseLocationActivity extends AppCompatActivity {

    //声明AMapLocationClient类对象
    public AMapLocationClient mLocationClient = null;
    //声明定位回调监听器
    public AMapLocationListener mLocationListener = null;

    OnLocationListener locationListener = null;

    public AMapLocationClientOption mLocationOption = null;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_choose_location);


        XXPermissions.with(this)
                .permission(Permission.ACCESS_FINE_LOCATION)
                .permission(Permission.ACCESS_COARSE_LOCATION)
                .request(new OnPermissionCallback() {

                    @Override
                    public void onGranted(List<String> permissions, boolean all) {
                        if (all) {
                            startLocation();
                        } else {
                            Utils.showToast("请授权定位");
                            finish();
                        }
                    }

                    @Override
                    public void onDenied(List<String> permissions, boolean never) {
                        Utils.showToast("请授权定位");
                        finish();
                    }
                });


    }

    private void startLocation() {

        mLocationClient = new AMapLocationClient(getApplicationContext());
        //设置定位回调监听
        mLocationListener = new AMapLocationListener() {
            @Override
            public void onLocationChanged(AMapLocation aMapLocation) {

                if (aMapLocation !=null && aMapLocation.getErrorCode() == 0) {
                    Utils.Sout( "地址" + aMapLocation.getAddress());
                    if (locationListener != null) {
                        String city = aMapLocation.getCity().substring(0, aMapLocation.getCity().length() - 1);
                        locationListener.onLocationChanged(new LocatedCity(city, aMapLocation.getProvince(), aMapLocation.getCityCode()), LocateState.SUCCESS);
                    }
                }
            }
        };
        mLocationClient.setLocationListener(mLocationListener);

        //初始化AMapLocationClientOption对象
        mLocationOption = new AMapLocationClientOption();

        mLocationOption.setLocationMode(AMapLocationClientOption.AMapLocationMode.Battery_Saving);
        mLocationOption.setOnceLocation(true);
        mLocationClient.setLocationOption(mLocationOption);

        chooseLocation();
    }

    private void chooseLocation() {
        List<HotCity> hotCities = new ArrayList<>();
        hotCities.add(new HotCity("北京", "北京", "101010100")); //code为城市代码
        hotCities.add(new HotCity("上海", "上海", "101020100"));
        hotCities.add(new HotCity("广州", "广东", "101280101"));
        hotCities.add(new HotCity("深圳", "广东", "101280601"));
        hotCities.add(new HotCity("杭州", "浙江", "101210101"));

        CityPicker.from(this)
                .enableAnimation(true)
                .setLocatedCity(null)
                .setHotCities(hotCities)
                .setOnPickListener(new OnPickListener() {

                    @Override
                    public void onPick(int position, City data) {
                        mLocationClient.stopLocation();
                        Utils.insertToSp("location", data.getName());
                        sendLiveEvent();
                        finish();
                    }

                    @Override
                    public void onLocate(OnLocationListener listener) {
                        locationListener = listener;
                        mLocationClient.startLocation();

                    }

                    @Override
                    public void onCancel() {
                        mLocationClient.stopLocation();
//                        Toast.makeText(getApplicationContext(), "取消选择", Toast.LENGTH_SHORT).show();
                        Utils.insertToSp("location", "");
                        sendLiveEvent();
                        finish();
                    }
                })
                .show();

    }

    private void sendLiveEvent() {

        Map<String, String> map = new HashMap<>();
        map.put("flag", "1");

        LiveEventBus
                .get("index_refresh")
                .post(new LiveEventMsg(map));
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        mLocationClient.onDestroy();
    }
}